// pages/tools/calculator/index/index.js
import util from "../../../../utils/util";
import WxValidate from "../.././../../utils/WxValidate"
const app = getApp();
var cityInfo = [];
var shebaoMax = 0;
var gongjijinMax = 0;
var shebaoMin = 0;
var gongjijinMin = 0;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        city: [],
        cityIndex: 0,
        type: ["职工", "居民"],
        typeIndex: 0,
        minNum: "",
        payMinNum: "",
        proportion: "",
        isPayMinNum: false,
        isMinNum: false,
        shebaoMin: 0,
        shebaoMax: 0,
        gongjijinMin: 0,
        gongjijinMax: 0,
        gjjSwitch: false,
        title: "",
        path: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        util.getUrl(app.globalData.url).then(res => {
            this.getCity();
        });
        this.initValidate();
    },

    getCity() {
        util.request("zhuanti.php", {
            action: "ss_type"
        }, ).then(res => {
            if (res.data.message == "success") {
                let city = [];
                res.data.data.forEach(item => city.push(item.cityname));
                cityInfo = res.data.data;
                this.setData({
                    city,
                    type: res.data.data[0].type,
                });
                this.getCityInfo();
            }
        })

    },

    citySelect(e) {
        let cityIndex = e.detail.value;
        let type = cityInfo[cityIndex].type;
        this.setData({
            type,
            cityIndex,
            typeIndex: 0,
        });
        this.getCityInfo();
    },

    typeSelect(e) {
        let typeIndex = e.detail.value;
        this.setData({
            typeIndex,
        });
        this.getCityInfo();
    },

    getCityInfo() {
        util.request("zhuanti.php", {
            action: "wuxianyijin",
            citycode: cityInfo[this.data.cityIndex].citycode,
            type: this.data.type[this.data.typeIndex],
        }, ).then(res => {
            if (res.data.message == "success") {
                shebaoMax = Number(res.data.data.ss.shebao.max);
                shebaoMin = Number(res.data.data.ss.shebao.min);
                gongjijinMax = Number(res.data.data.ss.gjj.max);
                gongjijinMin = Number(res.data.data.ss.gjj.min);
                this.setData({
                    shebaoMin,
                    shebaoMax,
                    gongjijinMin,
                    gongjijinMax,
                    title: res.data.data.ss.title,
                    path: res.data.data.ss.url,
                });
            }
        })
    },

    minBase(e) {
        let type = e.currentTarget.dataset.type;
        if (type === "pay")
            this.setData({
                isPayMinNum: !this.data.isPayMinNum,
                payMinNum: gongjijinMin,
            });
        else
            this.setData({
                isMinNum: !this.data.isMinNum,
                minNum: shebaoMin
            });

    },

    switchGjj(e) {
        this.setData({
            gjjSwitch: e.detail.value,
        })
    },

    baseNumInput(e) {
        let minNum = e.detail.value;
        let type = e.currentTarget.dataset.type;
        if (type == "pay") {
            if (minNum < gongjijinMin)
                this.setData({
                    payMinNum: gongjijinMin
                });
            else if (minNum > gongjijinMax)
                this.setData({
                    payMinNum: gongjijinMax,
                });
        } else if (type == "base") {
            if (minNum < shebaoMin)
                this.setData({
                    minNum: shebaoMin
                });
            else if (minNum > shebaoMax)
                this.setData({
                    minNum: shebaoMax,
                });
        } else {
            if (minNum < 5)
                this.setData({
                    proportion: 5
                });
            else if (minNum > 12)
                this.setData({
                    proportion: 12,
                });
        }
    },

    showWarnInfo(error) {
        ks.showModal({
            title: '提示',
            content: error.msg,
            showCancle: false,
        })
    },

    initValidate() {
        const rule = {
            baseNum: {
                required: true,
                min: 2200,
                max: 31938,
            },
        };
        const message = {
            baseNum: {
                required: "请输入参保基数",
                min: "请输入不小于2200的数字",
                max: "请输入不大于31938的数字"
            },
            payBaseNum: {
                required: "请输入缴存基数",
                min: "请输入不小于2200的数字",
                max: "请输入不大于31938的数字"
            },
            proportion: {
                required: "请输入缴存比例",
                min: "请输入不小于5的数字",
                max: "请输入不大于12的数字"
            }
        };
        //  创建实例对象
        this.WxValidate = new WxValidate(rule, message);
    },

    submit(e) {
        let parms = e.detail.value;
        if (!this.WxValidate.checkForm(parms)) {
            const error = this.WxValidate.errorList[0];
            this.showWarnInfo(error);
            return false;
        } else {
            parms.type = this.data.type[parms.type];
            parms.city = cityInfo.filter(item => item.cityname == this.data.city[parms.city])[0].citycode;
            ks.navigateTo({
                url: `/pages/tools/calculator/result/result?computeData=${JSON.stringify(parms)}`,
            });
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})